package cn.gailvlun.gll.presentation.hotel

import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.databinding.ObservableBoolean
import android.os.Bundle
import android.text.Html
import android.text.Spanned
import android.view.MotionEvent
import android.view.View
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityNightHotelBinding
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.hotel.InsomniaNumber
import cn.gailvlun.gll.net.hotel.InsomniaType
import cn.gailvlun.gll.presentation.base.App
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.hotel.compose.ComposePanelWindow
import cn.gailvlun.gll.presentation.hotel.compose.ComposeState
import cn.gailvlun.gll.presentation.hotel.drawer.HotelMyShareActivity
import cn.gailvlun.gll.presentation.hotel.night.HotelBrowseActivity
import cn.gailvlun.gll.util.DialogUtil
import cn.gailvlun.gll.util.RxUtil


class NightHotelActivity : BaseActivity() {

    companion object {
        fun openActivity(context: Context) {
            val intent = Intent(context, NightHotelActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var mBinding: ActivityNightHotelBinding
    private lateinit var mPresenter: Presenter

    private var maskHeight: Int? = null
    private lateinit var mComposePanelWindow: ComposePanelWindow

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(mContext, R.layout.activity_night_hotel)
        mPresenter = Presenter()
        mBinding.presenter = mPresenter

        mBinding.llMask.addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
            override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
                maskHeight = mBinding.llMask.measuredHeight
                mBinding.llMask.removeOnLayoutChangeListener(this)
            }
        })
        


















































        val pd = DialogUtil.showIndeterminate(mContext)
















        HttpMethods.getHotelService()
                .insomniaNumber
                .compose(RxUtil.applyScheduler())
                .compose(bindToLifecycle())
                .subscribe(object : HttpSubscriber<InsomniaNumber>() {
                    override fun onSuccess(response: InsomniaNumber) {
                        mBinding.item = response
                    }

                    override fun onFailure(errMsg: String, response: InsomniaNumber?, code: Int) {
                        DialogUtil.showError(mContext, errMsg)
                    }

                    override fun onFinish() {
                        super.onFinish()
                        pd.dismiss()
                    }
                })
    }

    override fun createHandler() {
        mBinding.nbv.setOnBackListener { onBackPressed() }
        mBinding.nbv.setOnMenuClickListener {
            HotelMyShareActivity.openActivity(mContext)
        }
    }

    override fun loadData(savedInstanceState: Bundle?) {

    }

    override fun onStart() {
        super.onStart()
        mComposePanelWindow = ComposePanelWindow(mContext)

    }
    inner class Presenter {
        var isComposeVisible = ObservableBoolean(true)
        fun onLook() {
            HotelBrowseActivity.openActivity(mContext)
        }

        fun onListen() {
            HotelBrowseActivity.openActivity(mContext, InsomniaType.V)
        }

        fun onEdit(view: View) {
            switchComposePanel(ComposeState.TO_OPEN)
        }
    }

    object Util {
        @JvmStatic
        fun getInsomniaNumberText(item: InsomniaNumber?): Spanned? {
            if (item == null) {
                return null
            }






















            return Html.fromHtml(App.getInstance().resources.getString(R.string.hotel_night_mask_note2, item.user_number, item.story_number))
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        return if (switchComposePanel(ComposeState.TO_CLOSE)) {
            true
        } else {
            super.dispatchTouchEvent(ev)
        }
    }

    override fun onPause() {
        super.onPause()
        switchComposePanel(ComposeState.TO_CLOSE)

    }

    private fun switchComposePanel(state: ComposeState): Boolean {
        when (state) {
            ComposeState.TO_CLOSE -> {

                return if (mComposePanelWindow.isShowing) {
                    mComposePanelWindow.dismiss()
                    mPresenter.isComposeVisible.set(true)
                    true
                } else {
                    false
                }
            }
            ComposeState.TO_OPEN -> {
                return if (!mComposePanelWindow.isShowing) {
                    mComposePanelWindow.show()
                    mPresenter.isComposeVisible.set(false)
                    true
                } else {
                    false
                }
            }

        }
    }

}